package com.aaa.member.service.impl;

import com.aaa.entity.Member;
import com.aaa.member.dao.RegisterDao;
import com.aaa.member.service.RegisterService;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 荆豪峰
 * @version 1.0
 * #Date: 2023/3/6 14:11
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterDao registerDao;

    @Override
    public Result insert(Member member) {
        List<Member> phone = registerDao.findPhone(member.getPhoneNum());
      List<Member> users = registerDao.findUserName(member.getUsername());
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode(member.getPassword());
        if (users.size()!=0 && phone.size()!=0){
            return new Result(3000,"该账号或手机号码已存在");
        }else {
            int i=  registerDao.insertAll(member.getUsername(),password,member.getPhoneNum());
            return new Result(2000,"注册成功");
        }
    }

    @Override
    public Result findPhone(String phoneNum) {
        List<Member> phone = registerDao.findPhone(phoneNum);
        if (phone.size() != 0){
            return new Result(2000,"该手机号码可用");
        }else {
            return new Result(5000,"该手机号码未注册");
        }
    }

    @Override
    public Result updatepswd(Member member) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode(member.getPassword());
        int i=   registerDao.updatepswd(password,member.getPhoneNum());
        return new Result(2000,"修改成功");
    }
}
